package io.gps.datatransfer.utils;

import org.apache.log4j.Logger;

import java.io.*;
import java.util.HashMap;
import java.util.Map;


/**
 * 文件夹，文件读取工具类
 * @author bcs
 * @version 2.0, $Date:  2020-11-26 13:04:48 $
 */
public class FileUtils {
    private static Logger logger = Logger.getLogger(FileUtils.class);
    public static String getConfigDirect() {
        FileUtils deleteDirectory = new FileUtils();
        String path = deleteDirectory.getProjectPath() + "config";

        File f = new File(path);
        boolean boo=false;
        if(!f.exists()){
            boo=f.mkdirs();
        }
        if(boo){
            return path;
        }
        return path;
    }

    //是否指定目录
    private boolean flag = false;

    // 默认采用Web项目下build文件夹与根目录的关系得到项目根目录的绝对路径
    public FileUtils() {
        flag = false;
    }

    // 利用字符串拼凑方法获取项目中的根目录结构
    public String getProjectPath() {
        String base = this.getClass().getResource("").getPath();

        // 字符分割
        String regex = "/";
        // 判断分隔符
        String[] temp = null;
        if (base.contains("\\")) {
            regex = "\\";
            temp = base.split(regex + regex);
        }
        if (base.contains("/")) {
            regex = "/";
            temp = base.split(regex);
        }
        StringBuffer destPath = new StringBuffer();
        String returnPathStr="";
        if(temp!=null&&temp.length>0){
            // 开头存在/ 去掉/前的空元素  否则不去掉
            int i = 0;
            if (temp[0].equals("")) {
                i = 1;
            } else {
                i = 0;
            }
            // 匹配目标文件夹
            for (; i < temp.length - 1; i++) {
                destPath.append(temp[i]);
                destPath.append(regex);
            }
            returnPathStr=destPath.toString();
            int lastidx = returnPathStr.indexOf(".jar!");
            if (lastidx > -1) {
                String filepath = returnPathStr.substring(0, lastidx);
                if (filepath.startsWith("file:/")) {
                    filepath = filepath.substring(6, filepath.length());
                }
                int lastidx1 = filepath.lastIndexOf("/");
                if ((lastidx1 + 1) < filepath.length()) {
                    if (lastidx1 > 0) {
                        returnPathStr = filepath.substring(0, lastidx1 + 1);
                    } else {
                        returnPathStr = "/";
                    }
                } else {
                    returnPathStr = filepath;
                }
            }
        }


        //Linux系统下加上开头"/"
        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.indexOf("linux") >= 0 && !returnPathStr.equals("/")) {
            returnPathStr = "/" + returnPathStr;
        }
        return returnPathStr;
    }


    public static void InitBlackList()throws IOException {

        try(BufferedReader bf = new BufferedReader(new FileReader(FileUtils.getConfigDirect()+"/BLACK_LIST.txt"))){
            String str;
            Map<String,String> tempList = new HashMap<String,String>();
            // 按行读取字符串
            while ((str = bf.readLine()) != null) {
                str= Utils.delSpaceNotStrNull(str);
                if(!"".equals(str)){
                    tempList.put(str,str);
                }
            }
            SysDef.BLACK_LIST=tempList;
        }
    }

    public static void InitCarList()throws IOException {

        try(BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(FileUtils.getConfigDirect()+"/CAR_LIST.txt"), "UTF-8"))){
            String str;
            Map<String,Map<String,String>> tempList = new HashMap<String,Map<String,String>>();
            // 按行读取字符串
            while ((str = bf.readLine()) != null) {
                str= Utils.delSpaceNotStrNull(str);
                if(!"".equals(str)){
                    Map<String,String> tempcar=new HashMap<>();
                    String[] temp =str.split(",");
                    if(temp.length>2){
                        String id=Utils.delSpaceNotStrNull(temp[0]);
                        tempcar.put("SIM",id);
                        tempcar.put("CPH",Utils.delSpaceNotStrNull(temp[1]));
                        tempcar.put("CPYS",Utils.delSpaceNotStrNull(temp[2]));
                        if(temp.length>4){
                            tempcar.put("SHENG",Utils.delSpaceNotStrNull(temp[3]));
                            tempcar.put("SHI",Utils.delSpaceNotStrNull(temp[4]));
                        }
                        tempList.put(id,tempcar);
                    }
                }
            }
            SysDef.CAR_LIST=tempList;
        }
    }

}
